//
//  BSSBankListViewController.m
//  BSSMarket
//
//  Created by 传铎 on 2020/9/25.
//  Copyright © 2020 BSS. All rights reserved.
//

#import "BSSBankListViewController.h"
#import "NoDataView.h"
@interface BSSBankListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView            * headView;
@end

@implementation BSSBankListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.title = @"选择银行";
//    self.navBar.titleLabel.textColor = colorCode(@"333333");
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = colorCode(@"f2f2f2");
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatUI];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.001f;
}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.00001f)];
//    return view;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellID" forIndexPath:indexPath];
    cell.textLabel.text = _dataArray[indexPath.row];
    cell.textLabel.textColor = colorCode(@"#333333");
    cell.textLabel.font = fontCount(17);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (NSString *)timeConversionC:(NSString *)time{
    if (IsEmpty(time)) {
        return @"";
    }
    NSString *str;
    if (time.length >=10) {
        str = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",[time substringWithRange:NSMakeRange(0, 4)],[time substringWithRange:NSMakeRange(4, 2)],[time substringWithRange:NSMakeRange(6, 2)],[time substringWithRange:NSMakeRange(8, 2)],[time substringWithRange:NSMakeRange(10, 2)]];
        return str;
    }else{
        return time;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _black(_dataArray[indexPath.row]);
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, SCREEN_HEIGHT - __kNavigationBarHeight__ - __kTabbarSafeBottomMargin__)
                                              style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 200.0f;
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
 
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellID"];
    [self.view addSubview:_tableView];
    //适配
    if (@available(iOS 13.0, *)) {
        [UICollectionView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }else if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.headView   = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190)];
    
    NoDataView * _noDataView = [[NoDataView alloc] initXGViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190)];
    _noDataView.backImage.image = [UIImage imageNamed:@"BSS_NoData"];
    _noDataView.titleLabel.text = @"暂时没有数据呐";
    _noDataView.backView.backgroundColor = [UIColor clearColor];
    _noDataView.backgroundColor = [UIColor clearColor];
    [self.headView addSubview:_noDataView];
    WEAKSELF
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getMessageType:@"new"];
    }];
//    
//    self.tableView.mj_footer = [BSSRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf getMessageType:@"more"];
//    }];
    [_tableView.mj_header beginRefreshing];
}
- (void)getMessageType:(NSString *)messageType{
    NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
//    [jsonDic setObject:@"10" forKey:@"pageSize"];
//    [jsonDic setObject:@"1" forKey:@"pageNo"];
//
//    if (![messageType isEqualToString:@"new"]) {
//        [jsonDic setObject:[NSString stringWithFormat:@"%zi",self.dataArray.count/10 + 1] forKey:@"pageNo"];
//    }
    WEAKSELF;
    BSSRequest * request = [[BSSRequest alloc] initWithURL:@"" dic:jsonDic];
    [request startRequest:^(BSSRequestStatus requestStatus, NSString * _Nullable message, id  _Nullable responseObject) {
        if (requestStatus == BSSRequestStatusSuccess) {
            if ([messageType isEqualToString:@"new"]) {
                [weakSelf.dataArray removeAllObjects];
                [weakSelf.tableView.mj_header endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            if (!IsEmpty(responseObject[@"data"])) {
                [weakSelf.dataArray addObjectsFromArray:responseObject[@"data"]];
            }
            if (IsEmpty(weakSelf.dataArray)) {
                self.tableView.tableHeaderView = self.headView;
            }else{
                self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.00001)];
            }
            
//            if ([responseObject[@"data"] count] < 10) {
//                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//                weakSelf.tableView.mj_footer.hidden = YES;
//            }else{
//                [weakSelf.tableView.mj_footer resetNoMoreData];
//                weakSelf.tableView.mj_footer.hidden = NO;
//            }
            [self.tableView reloadData];
        } else {
            if ([messageType isEqualToString:@"new"]) {
                
                [weakSelf.tableView.mj_header endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            [MBProgressHUD showText:message];
            
        }
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
